package com.food.controller;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

import com.food.dao.service.CourseService;
import com.food.dao.service.MemberService;
import com.food.dao.service.ProductService;
import com.food.model.Course;
import com.food.model.Member;
import com.food.model.Product;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/manager/ProductServlet")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		
		String type = request.getParameter("type");
		if(type!=null && type.equals("DELETE")){
			int pid = Integer.parseInt(request.getParameter("pid"));
			Product p = new Product();
			p.setPid(pid);
			ProductService productService = new ProductService();
			productService.deleteProduct(p);
			RequestDispatcher rd = request
					.getRequestDispatcher("/shopping/DisplayProduct?action=serch_all&pageNumber=0&type=MANAGER");
			rd.forward(request, response);
			return;
			
		}
		
		String pname = request.getParameter("pname");
		String pcooker = request.getParameter("pcooker");
		String pcountry = request.getParameter("pcountry");
		String ppath = request.getParameter("ppath");
		String pintroduce = request.getParameter("pintroduce");
		
		
		Product p = new Product();
		ProductService productService = new ProductService();
		p.setPname(pname);
		p.setPcooker(pcooker);
		p.setPcountry(pcountry);
		p.setPpath(ppath);
		p.setPintroduce(pintroduce);
		
		productService.insertProduct(p);
		
		RequestDispatcher rd = request
				.getRequestDispatcher("/shopping/DisplayProduct?action=serch_all&pageNumber=0&type=MANAGER");
		rd.forward(request, response);
		return;
	
	}

}
